﻿using Godot;
using System.Collections.Generic;

public class TransformBox
{
    public class Target
    {
        public UIElement Element;
        public float XMin, XMax, YMin, YMax;
    }

    public TransformBox()
    {
        Targets = new List<Target>();
    }

    public List<Target> Targets { get; }

    public bool HasTargets => Targets.Count > 0;

    public void SetTargets(Vector2 offset, float scale, IReadOnlyList<UIElement> targets)
    {
        Targets.Clear();
        if (targets.Count > 0)
        {
            XMin = YMin = float.MaxValue;
            XMax = YMax = float.MinValue;
            for (var i = 0; i < targets.Count; i++)
            {
                var target = targets[i];
                var rect = target.GetGlobalRect();
                rect.Position -= offset;
                rect.Size *= scale;
                if (XMin > rect.Position.x)
                    XMin = rect.Position.x;
                if (XMax < rect.End.x)
                    XMax = rect.End.x;
                if (YMin > rect.Position.x)
                    YMin = rect.Position.x;
                if (YMax < rect.End.y)
                    YMax = rect.End.y;
                Targets.Add(new Target()
                {
                    Element = target,
                });
            }
            /*for (var i = 0; i < Targets.Count; i++)
            {
                target = targets[0];
                var rect = target.GetGlobalRect();
                rect.Position -= offset;
                rect.Size *= scale;
                if (XMin > rect.Position.x)
                    XMin = rect.Position.x;
                if (XMax < rect.End.x)
                    XMax = rect.End.x;
                if (YMin > rect.Position.x)
                    YMin = rect.Position.x;
                if (YMax < rect.End.y)
                    YMax = rect.End.y;
                Targets.Add(new Target()
                {
                    Element = target,
                });
            }*/
        }
        else
        {
            XMin = YMin = XMax = YMax = 0;
        }
    }

    public float XMin { get; set; }
    public float YMin { get; set; }
    public float XMax { get; set; }

    internal void OnMouseEvent(InputEventMouse e)
    {
        
    }

    public float YMax { get; set; }
    public Rect2 Rect => RectUtil.FromMinMax(XMin, YMin, XMax, YMax);

    public void Bein(InputEventMouse e, OperatorModeType op)
    {
        switch (op)
        {
            case OperatorModeType.Vsize:

                break;
            case OperatorModeType.Hsize:
                break;
            case OperatorModeType.Bdiagsize:
                break;
            case OperatorModeType.Fdiagsize:
                break;
            case OperatorModeType.Move:
                break;
        }
    }

    public void End(InputEventMouse e, OperatorModeType op)
    {
        
    }

    private void UpdateTargets()
    {


    }
}
